package com.example.infowall02.controller;

import com.example.infowall02.domain.Articles;
import com.example.infowall02.domain.FavoriteRequest;
import com.example.infowall02.domain.User;
import com.example.infowall02.mapper.ArticlesMapper;
import com.example.infowall02.mapper.UserMapper;
import com.example.infowall02.service.FavoriteService;
import com.example.infowall02.service.UserService;
import com.example.infowall02.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private UserService userService;
    @Autowired
    private FavoriteService favoriteService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticlesMapper articlesMapper;

    // 添加收藏
    @PutMapping
    public Result addFavorite(@RequestBody FavoriteRequest favoriteRequest) {
        int userId = favoriteRequest.getUserId();
        int articleId = favoriteRequest.getArticleId();
        // 检查用户是否存在
        int userCount = userMapper.countUserById(userId);
        if (userCount == 0) {
            return Result.error("用户不存在，收藏失败");
        }
        // 检查文章是否存在
        int articleCount = articlesMapper.countArticleById(articleId);
        if (articleCount == 0) {
            return Result.error("文章不存在，收藏失败");
        }
        // 检查用户是否已经收藏过
        boolean isFavorited = favoriteService.isUserFavoritedArticle(userId, articleId);
        if (isFavorited) {
            return Result.error("你已经收藏过了，无需重复收藏");
        }
        // 用户和文章都存在，且未收藏过，执行收藏操作
        favoriteService.addFavorite(userId, articleId);
        return Result.success("成功收藏");
    }
    // 取消收藏
    @DeleteMapping
    public Result deleteFavorite(@RequestBody FavoriteRequest favoriteRequest) {
        int userId = favoriteRequest.getUserId();
        int articleId = favoriteRequest.getArticleId();
        // 检查用户是否存在
        int userCount = userMapper.countUserById(userId);
        if (userCount == 0) {
            return Result.error("用户不存在，取消收藏失败");
        }
        // 检查文章是否存在
        int articleCount = articlesMapper.countArticleById(articleId);
        if (articleCount == 0) {
            return Result.error("文章不存在，取消收藏失败");
        }
        // 检查用户是否已经收藏过
        boolean isFavorited = favoriteService.isUserFavoritedArticle(userId, articleId);
        if (isFavorited) {
            favoriteService.deleteFavorite(userId, articleId);
            return Result.success("成功取消收藏");
        }
        return Result.error("你还没有收藏过，无法取消收藏");
    }
    // 获取用户收藏数
    @GetMapping("/count/{userId}")
    public Result countUserFavorites(@PathVariable int userId) {
        // 检查用户是否存在
        User user = userService.findUserById(userId);
        if (user == null) {
            return Result.error("用户不存在，获取失败");
        }
        // 获取用户收藏数
        int count = favoriteService.getUserFavoriteCount(userId);
        return Result.success(count);
    }
    // 获取用户的收藏文章列表
    @GetMapping("/{userId}")
    public Result<List<Articles>> userFavoriteArticleList(@PathVariable int userId) {
        User user = userService.findUserById(userId);
        if (user != null) {
            List<Articles> articles = favoriteService.getUserFavoriteArticles(userId);
            return Result.success(articles);
        }
        return Result.error("用户不存在，获取失败");
    }

}
